<template>
  <el-dialog :visible="showDiaglog" title="新增员工" @close="cancelBtn">
    <el-form ref="addForm" :model="formData" :rules="formRules" label-width="120px">
      <el-form-item label="姓名" style="width:50%" prop="username">
        <el-input v-model="formData.username" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item label="手机" style="width:50%" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" style="width:50%" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="选择日期" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="聘用形式" style="width:50%" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" style="width:50%" prop="workNumber">
        <el-input v-model="formData.workNumber" placeholder="1-20个字符" />
      </el-form-item>
      <el-form-item label="部门" style="width:50%" prop="departmentName">
        <el-input v-model="formData.departmentName" v-loading="loading" placeholder="请选择" @focus="getDeparts" />
        <el-tree v-if="showTree" :data="treeData" :props="{ label:'name'}" :default-expand-all="true" @node-click="selectNode" />
      </el-form-item>
      <el-form-item label="转正时间" style="width:50%" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" type="date" placeholder="选择日期" style="width: 100%;" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center" align="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="confirmBtn">提交</el-button>
        <el-button size="small" @click="cancelBtn"> 取消</el-button>

      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartment } from '@/api/department'
import { transDataList } from '@/utils/index'
import EmployeeEnum from '@/api/constant/employees'
import { addEmployees } from '@/api/employee'
export default {
  name: '',
  components: {

  },
  props: {
    showDiaglog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      formRules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户名格式不能超过1-4位', trigger: 'blur' }
        ],
        mobile: [{ required: true, message: '用户名不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '用户名不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '用户名不能为空', trigger: 'blur' }]
      },
      treeData: [],
      showTree: false,
      loading: false
    }
  },
  computed: {},
  watch: {},
  created() {

  },
  mounted() {},
  methods: {
    async getDeparts() {
      this.loading = true
      this.showTree = true

      const { depts } = await getDepartment()
      this.treeData = transDataList(depts, '')
      this.loading = false
    },
    selectNode(node) {
      console.log(node)
      this.formData.departmentName = node.name
      this.showTree = false
    },
    confirmBtn() {
      this.$refs.addForm.validate(async isOk => {
        if (isOk) {
          try {
            await addEmployees(this.formData)
            this.$refs.addForm.resetFields()
            this.$emit('addEmploy', false)
          } catch (err) {
            console.log(err)
          }
        }
      })
    },
    cancelBtn() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.addForm.resetFields()
      this.$parent.showDiaglog = false
    }
  }
}
</script>

<style scoped>

</style>
